package com.offer;

import com.bean.TreeNode;
import com.utils.DataUtils;
import com.utils.TreeUtils;

import java.lang.reflect.Array;
import java.util.*;

public class Test32_1 {

    public List<List<Integer>> levelOrder(TreeNode root) {
        ArrayList<List<Integer>> result = new ArrayList<>();

        Queue<TreeNode> queue = new LinkedList<>();

        if(root!=null) {
            queue.add(root);
        }

        while (!queue.isEmpty()) {
            int size = queue.size();

            ArrayList<Integer> arrayList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode treeNode = queue.poll();

                if (treeNode == null) {
                    continue;
                }

                arrayList.add(treeNode.val);

                if (treeNode.left != null) {
                    queue.offer(treeNode.left);
                }

                if (treeNode.right != null) {
                    queue.offer(treeNode.right);
                }
            }
            result.add(arrayList);
        }
        return result;
    }


    public static void main(String[] args) {
        TreeNode root = TreeUtils.buildTree("3,9,20,null,null,15,7");
        TreeUtils.showTree(root);

        Test32_1 test32 = new Test32_1();
        List<List<Integer>> result = test32.levelOrder(root);
        DataUtils.printListListInteger(result);


    }

}
